package com.mhkj.base.busiMode.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.mhkj.base.busiMode.entity.BusiModel;
import com.mhkj.base.busiMode.repository.BusiModelRepository;
import com.mhkj.base.busiMode.service.BusiModelService;
import com.mhkj.base.busiType.service.BusinessTypeService;
import com.mics.core.common.componet.Pagination;
import com.mics.core.system.base.service.impl.BaseServiceImpl;
import com.mics.core.system.organizational.bean.Employee;
import com.mics.core.system.organizational.service.OrgService;
import com.mics.core.system.rbac.entity.Dictionary;
import com.mics.core.system.rbac.service.DictionaryService;

/**
 * 业务系统模块
 * @author mics
 * @date 2018年7月9日
 * @version 1.0
 */
@Service
public class BusiModelServiceImpl extends BaseServiceImpl<BusiModel> implements BusiModelService{
	@Resource
	private BusiModelRepository busiModelRepository;
	@Resource
	private BusinessTypeService businessTypeService;
	@Resource
	private DictionaryService  dictionaryService;
	@Resource
	private OrgService orgService;
	
	@Resource
	public void setBusiModelRepository(BusiModelRepository busiModelRepository) {
		super.setBaseRepository(busiModelRepository);
	}

	@Override
	public Pagination<BusiModel> listByPage(BusiModel busiModel, int pageIndex,int pageSize) {
		Specification<BusiModel> spec = new Specification<BusiModel> () {  
			/**
			 * 序列号
			 */
			private static final long serialVersionUID = -786003485941679959L;
			@Override
			public Predicate toPredicate(Root<BusiModel> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
              List<Predicate> lstPredicates = new ArrayList<Predicate>();
              if(StringUtils.hasText(busiModel.getName())){
                  Predicate p1=criteriaBuilder.like(root.get("name").as(String.class), "%"+busiModel.getName()+"%");
                  lstPredicates.add(p1);
              }
              Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
              return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
	        }
		};
		Page<BusiModel> pageT =  busiModelRepository.findAll(spec, PageRequest.of(pageIndex, pageSize, Sort.Direction.ASC,"id"));
		Pagination<BusiModel> pagination = new Pagination<BusiModel>();
		pagination.setTotal(busiModelRepository.count(spec));
		List<BusiModel> busiModels = pageT.getContent();
		for(BusiModel busiModel2:busiModels){
			Dictionary busiSys = dictionaryService.getById(busiModel2.getBusiSysId());
			busiModel2.setBusiSysName(busiSys.getName());
			Dictionary phase = dictionaryService.getById(busiModel2.getPhaseId());
			busiModel2.setPhaseName(phase.getValue());
			Employee creEmployee = orgService.employeeInfo(busiModel2.getCreateBy());
			busiModel2.setCreateUserName(creEmployee.getName());
		}
		pagination.setData(busiModels);
		return pagination;
	}

	@Override
	public BusiModel getByName(String modeName) {
		BusiModel busiModel = busiModelRepository.getByName(modeName);
		return busiModel;
	}
	
	
}
